package com.ruoyi.mall.product.interfaces.facade;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mall.product.domain.entity.PmsAttr;
import com.ruoyi.mall.product.domain.entity.PmsSkuAttrValue;
import com.ruoyi.mall.product.domain.vo.PmsAttrVo;
import com.ruoyi.mall.product.service.PmsAttrAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品属性Controller
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@Api(tags = "商品属性")
@RestController
@RequestMapping("/attr")
public class PmsAttrController extends BaseController {
    @Autowired
    private PmsAttrAppService pmsAttrAppService;

    /**
     * 查询商品属性列表
     */
    @ApiOperation("查询商品属性列表")
    @RequiresPermissions("product:attr:list")
    @GetMapping("{attrType}/list")
    public TableDataInfo list(@ApiParam(value = "修改商品属性", required = true) PmsAttr pmsAttr, @PathVariable("attrType") String attrType) {
        startPage();
        List<PmsAttr> list = pmsAttrAppService.selectPmsAttrList(pmsAttr, attrType);
        return getDataTable(list);
    }

    /**
     * 查询spu规格
     */
    @ApiOperation("查询spu规格")
    @GetMapping("/base/listForSpu/{spuId}")
    public AjaxResult baseAttrListForSpu(@ApiParam(value = "spuId", required = true) @PathVariable("spuId") Long spuId) {
        List<PmsSkuAttrValue> list = pmsAttrAppService.baseAttrListForSpu(spuId);
        return AjaxResult.success(list);
    }

    /**
     * 导出商品属性列表
     */
    @ApiOperation("导出商品属性列表")
    @RequiresPermissions("product:attr:export")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("{attrType}/export")
    public void export(HttpServletResponse response, PmsAttr pmsAttr, @PathVariable("attrType") String attrType) {
        List<PmsAttr> list = pmsAttrAppService.selectPmsAttrList(pmsAttr, attrType);
        ExcelUtil<PmsAttr> util = new ExcelUtil<PmsAttr>(PmsAttr.class);
        util.exportExcel(response, list, "商品属性数据");
    }

    /**
     * 获取商品属性详细信息
     */
    @ApiOperation("获取商品属性详细信息")
    @RequiresPermissions("product:attr:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "商品属性id查询", required = true) @PathVariable("id") Long id) {
        return AjaxResult.success(pmsAttrAppService.selectPmsAttrById(id));
    }

    /**
     * 新增商品属性
     */
    @ApiOperation(" 新增商品属性")
    @RequiresPermissions("product:attr:add")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "添加商品属性", required = true) @RequestBody PmsAttrVo pmsAttr) {
        return toAjax(pmsAttrAppService.insertPmsAttr(pmsAttr));
    }

    /**
     * 修改商品属性
     */
    @ApiOperation(" 修改商品属性")
    @RequiresPermissions("product:attr:edit")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "修改商品属性", required = true) @RequestBody PmsAttrVo pmsAttr) {
        return toAjax(pmsAttrAppService.updatePmsAttr(pmsAttr));
    }

    /**
     * 修改规格属性
     */
    @ApiOperation(" 修改规格属性")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping("/update/{spuId}")
    public AjaxResult updateSpuAttr(@ApiParam(value = "spuId", required = true) @PathVariable("spuId") Long spuId,
                                    @ApiParam(value = "sku属性值集合", required = true) @RequestBody List<PmsSkuAttrValue> list) {
        return toAjax(pmsAttrAppService.updatePmsAttrValue(spuId, list));
    }


    /**
     * 修改商品属性状态
     */
    @ApiOperation(" 修改商品属性状态")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult editStatus(@ApiParam(value = "修改商品属性状态", required = true) @RequestBody PmsAttr pmsAttr) {
        return toAjax(pmsAttrAppService.updateStatusPmsAttr(pmsAttr));
    }


    /**
     * 删除商品属性
     */
    @ApiOperation(" 删除商品属性")
    @RequiresPermissions("product:attr:remove")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "删除id集合", required = true) @PathVariable Long[] ids) {
        return toAjax(pmsAttrAppService.deletePmsAttrByIds(ids));
    }
}
